import 'package:acgn_pioneer/components/birthday.dart';
import 'package:acgn_pioneer/plugin/dialog/src/constants.dart';
import 'package:acgn_pioneer/plugin/dialog/src/dialog.dart';

import '../public.dart';

/// 编辑资料
class MyEditPage extends StatefulWidget {
  MyEditPage({Key key}) : super(key: key);

  @override
  _MyEditPageState createState() {
    return _MyEditPageState();
  }
}

class _MyEditPageState extends State<MyEditPage> {
  UserProvider user;
  ThemeProvider theme;
  String ubirthday;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      setState(() {
        ubirthday = UDateUtil.getBirthdayString(DateTime.fromMillisecondsSinceEpoch(user.data.ubirthday));
      });
    });
  }

  _onPressedBirthday(res) {
    setState(() {
      ubirthday = UDateUtil.getBirthdayString(res);
    });
  }

  @override
  Widget build(BuildContext context) {
    // 使用Provider
    user = Provider.of<UserProvider>(context);
    theme = Provider.of<ThemeProvider>(context);
    return Scaffold(
        backgroundColor: theme.primaryColor,
        appBar: AppBar(
          title: Text('编辑资料'),
          leadingWidth: 44,
          leading: BackComponent(),
          backgroundColor: theme.primaryColor,
          centerTitle: true,
          actions: <Widget>[],
        ),
        body: SingleChildScrollView(
            child: Padding(
                padding: EdgeInsets.only(left: 16, right: 16),
                child: Column(children: [
                  MenuComponent(
                      title: '头像',
                      decoration: BoxDecoration(),
                      onTap: () {
                        ACGN.toast.message('编辑头像');
                      },
                      rightWidgets: [
                        Container(
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(32),
                                border: Border.all(
                                  width: 1,
                                  color: ColorUtil.rgba(255, 255, 255, 1),
                                )),
                            child: ClipRRect(
                                borderRadius: BorderRadius.circular(32),
                                child: ImagesNetworkComponent(user.data?.uhead,
                                    placeholder:
                                        'assets/images/mrtx_qumh88.png',
                                    width: 32,
                                    height: 32))),
                      ]),
                  MenuComponent(
                      title: '昵称',
                      decoration: BoxDecoration(),
                      onTap: () {
                        ACGN.toast.message('编辑昵称');
                      },
                      rightWidgets: [
                        Text(user.data.uname,
                            style: TextStyle(color: Colors.white, fontSize: 16))
                      ]),
                  MenuComponent(
                      title: '签名',
                      decoration: BoxDecoration(),
                      onTap: () {
                        ACGN.toast.message('编辑签名');
                      },
                      rightWidgets: [
                        Text(
                            user.data.usign.isEmpty
                                ? '这个家伙很懒，什么都没留下'
                                : user.data.usign,
                            style: TextStyle(color: Colors.white, fontSize: 16))
                      ]),
                  MenuComponent(
                      title: '性别',
                      decoration: BoxDecoration(),
                      onTap: () {
                        ACGN.toast.message('编辑性别');
                      },
                      rightWidgets: [
                        Text(['未知', '男', '女', '保密'][user.data.usex],
                            style: TextStyle(color: Colors.white, fontSize: 16))
                      ]),
                  MenuComponent(
                      title: '生日',
                      decoration: BoxDecoration(),
                      onTap: () {
                        ACGNDialog(context,
                                backgroundColor: theme.primaryColor,
                                clipBehavior: Clip.hardEdge,
                                alignment: Alignment.bottomCenter,
                                transition: DialogTransition(
                                    type: AnimationType.fromBottom),
                                body: DialogBody(
                                    padding: EdgeInsets.only(
                                        left: 0, right: 0, top: 0, bottom: 30),
                                    content: BirthdayPicker(
                                        onPressed: _onPressedBirthday,
                                        initialDateTime:
                                            DateTime.fromMillisecondsSinceEpoch(
                                                user.data.ubirthday))))
                            .show();
                      },
                      rightWidgets: [
                        Text('$ubirthday',
                            style: TextStyle(color: Colors.white, fontSize: 16))
                      ]),
                  SizedBox(
                    height: 40,
                  ),
                  Text('ID: ${user.data.uid}',
                      style: TextStyle(color: Colors.white38, fontSize: 16))
                ]))));
  }
}
